from exts import db
from flask import Blueprint, request,render_template, url_for, redirect
import json

from student.model import Student
from user.model import User


student = Blueprint('student', __name__);

# 学生列表
@student.route('student_list', methods=['GET', 'POST'])
def student_list():
    student_list = Student.query.all()
    return render_template('student_list.html', student_list=student_list)

# 根据名称搜索
@student.route('/student/search', methods=['GET', 'POST'])
def search():
    keyword = request.args.get('keyword')
    class_name = request.args.get('class_name')
    number = request.args.get('number')
    dept = request.args.get('dept')
    phone = request.args.get('phone')
    if class_name is not None and keyword is not None and number is not None and dept is not None and phone is not None:
        student_list = Student.query.filter(
            Student.name.like('%{keyword}%'.format(keyword=keyword)),
            Student.class_name.like('%{class_name}%'.format(class_name=class_name)),
            Student.number.like('%{number}%'.format(number=number)),
            Student.dept.like('%{dept}%'.format(dept=dept)),
            Student.phone.like('%{phone}%'.format(phone=phone)),
        ).all()
    elif keyword is not None and number is not None and dept is not None and phone is not None:
        student_list = Student.query.filter(
            Student.name.like('%{keyword}%'.format(keyword=keyword)),
            Student.number.like('%{number}%'.format(number=number)),
            Student.dept.like('%{dept}%'.format(dept=dept)),
            Student.phone.like('%{phone}%'.format(phone=phone))
        ).all()
    else:
        student_list = Student.query.filter(
            Student.class_name.like('%{class_name}%'.format(class_name=class_name))
        ).all()
    return render_template('student_list.html', student_list=student_list)

# 删除学生
@student.route('/student/remove', methods=['GET', 'POST'])
def remove():
    id = request.args.get('id')
    student = Student.query.filter(
        Student.id == id
    ).first()
    db.session.delete(student)
    db.session.commit()
    return redirect(url_for('student.student_list'))

# 查看，更新，添加学生
@student.route('/student/edit', methods=['GET', 'POST'])
def edit():
    # 如果是get跳转详情页面，否则添加
    if request.method=='GET':
        id = request.args.get('id')
        option = request.args.get('option')
        student = Student.query.filter(
            Student.id == id
        ).first()
        if option == 'detail':
            return render_template('student_editor.html', student=student)
        else:
            return render_template('student_editor.html', student=student, update=True)
    else:
        id = request.form.get('id')
        name = request.form.get('name')
        number = request.form.get('number')
        class_name = request.form.get('class_name')
        phone = request.form.get('phone')
        age = request.form.get('age')
        sex = request.form.get('sex')
        dept = request.form.get('dept')
        student = Student.query.filter(
            Student.id == id
        ).first()
        # 如果存在用户，那么更新，不存在添加
        if student is None:
            student = Student(name=name, number=number,class_name=class_name,phone=phone, age=age, sex=sex, dept=dept)
            db.session.add(student)
            db.session.commit()
        else:
            student.name = name
            student.number = number
            student.class_name = class_name
            student.phone = phone
            student.age = age
            student.sex = sex
            student.dept = dept
            db.session.commit()
        return redirect(url_for("student.student_list"))

